import { useTokenStore } from '../stores/token.js'
import request from '../utils/request.js'


// 帖子创建
export const createPostService = async (postData) => {
    const tokenStore = useTokenStore() // 获取 token 状态
    try {
        const response = await request.post('/community/post/create', postData, {
            headers: {
                'Content-Type': 'application/json',
                'Authorization': `Bearer ${tokenStore.token}` 
            }
        });
        return response;
    } catch (error) {
        console.error('帖子创建失败:', error);
        throw error;
    }
}

// 获取帖子详情
export const getPostDetail = async (id) => {
    const tokenStore = useTokenStore()
    try {
        const response = await request.get(`/detail/${id}`, {
            headers: {
                'Authorization': `Bearer ${tokenStore.token}`
            }
        });
        return response.data;
    } catch (error) {
        console.error('获取帖子详情失败:', error);
        throw error;
    }
}

// 更新帖子
export const updatePost = async (postData) => {
    const tokenStore = useTokenStore()
    try {
        const response = await request.post('/update', postData, {
            headers: {
                'Content-Type': 'application/json',
                'Authorization': `Bearer ${tokenStore.token}`
            }
        });
        return response.data;
    } catch (error) {
        console.error('更新帖子失败:', error);
        throw error;
    }
}

// 删除帖子
export const deletePost = async (id) => {
    const tokenStore = useTokenStore()
    try {
        const response = await request.delete(`/delete/${id}`, {
            headers: {
                'Authorization': `Bearer ${tokenStore.token}`
            }
        });
        return response.data;
    } catch (error) {
        console.error('删除帖子失败:', error);
        throw error;
    }
}
